<template>
  <el-dialog
    title="条件/动作"
    :close-on-click-modal="false"
    width="60%"
    append-to-body
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm">


      <!-- 条件为已定义条件-->
      <el-row v-if="dataForm.conditionType=='4'">
        <el-col :span="24" class="mr10">
          <el-form-item label="因子" label-width="120px" prop="">
            <el-col :span="5">
              <el-select v-model="dataForm.divisorId" filterable clearable placeholder="请选择因子">
                <el-option
                  v-for="item in conditionDivisorList"
                  :key="item.id"
                  :label="item.divisorName"
                  :value="item.id"
                  @click.native="changeDivisor(item)">
                </el-option>
              </el-select>
            </el-col>

          </el-form-item>
        </el-col>
      </el-row>



      <el-row>
        <el-col :span="24">
          <el-form-item label="表达式" label-width="120px" prop="">
            <el-input type="textarea" :rows="3" v-model="dataForm.conditionExpression"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="描述" label-width="120px" prop="">
            <el-input type="textarea" :rows="3" v-model="dataForm.conditionDesc"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="getExp()">确定</el-button>
    </span>


  </el-dialog>


</template>

<script>
    import {entityItemList, getEntityMethod, entityList,getActionMethodName} from '@/api/baseConfig/entity'
    import constant from "../../../../../api/constant";
    import {listConditionDivisorData,getData} from '@/api/baseConfig/conditionDivisor'
    import {listActionDivisorData} from '@/api/baseConfig/actionDivisor'

    export default {
        data() {
            return {
                newConditionList:[],
                divisor: {
                    configDate: null,
                    configUser: null,
                    creTime: null,
                    creUserId: null,
                    divisorIdentify: null,
                    divisorMethod: null,
                    divisorName: null,
                    divisorType: null,
                    entityId: null,
                    id: null,
                    isEffect: null,
                    itemId: null,
                    itemIdentify: null,
                    itemTxtLength: null,
                    itemTxtType: null,
                    itemValueType: null,
                    methodDesc: null,
                    methodExpression: null,
                    methodParams: null,
                    methodType: null,
                    remark: null,
                    sysbol: null
                },
                divisorEnumList:[],
                divisorMethodParams:[],
                descData: [],
                dataForm: {
                    decisionCode:'',
                    headType:'',
                    conditionType: '',
                    divisorId: null,
                    leftProperty: null,
                    leftEntityId: null,
                    conditionExpression: '',
                    conditionDesc: '',
                    conditionMethodType: '',
                    methodParams: '',
                    methodName: '',
                },
                showParams: false,
                methodParams: [],
                ruleInfo: {},
                ruleCode: '',
                delInfo: [],
                inputRemark: '',
                rightValue: '',
                curEntity: {},
                methodList: [],
                entityEnum: '',
                enumList: [],
                enumFlag: false,
                entity: null,
                entityItem: null,
                ruleId: null,
                inputV: '',
                listData: [],
                entityInfos: [],
                entityItemInfos: [],
                leftEntityItemInfos: [],
                rightEntityItemInfos: [],
                visible: false,
                dialogVisible: false,
                sysbols: [],
                conditionTyps: constant.ruleConfig.conditionTyps,
                conditionDivisorList: []
            }
        },
        props: {
            conditionList: {
                type: Array, //指定传入的类型
                default: [] //这样可以指定默认的值
            },
            entityList: {
                type: Array, //指定传入的类型
                default: [] //这样可以指定默认的值
            },
            chooseIndex: {
                type: Number,
                default: 0
            },
            isconflict: {
              type: Boolean,
            }
        },
        created() {
           
        },
        methods: {
            changeDivisorParams(data){
                // console.log(data)
            },
            // 选择条件因子
            changeDivisor(data) {
                // console.log(data)
                this.divisor = data
                this.divisorMethodParams = []
                this.descData = new Array(2)

                //判断因子类型是方法还是非方法
                if (data.divisorType === '0') {
                    //非方法 枚举
                    if (data.itemValueType === '2') {
                        getData({id:data.id}).then(res=>{
                          this.divisorEnumList = res.data.data.enumList
                            // console.log(this.divisorEnumList)
                        })
                    }
                    let desc = JSON.parse(data.remark)
                    let s = desc[1].k.substring(0, 1).toLocaleUpperCase() + desc[1].k.substring(1, desc[1].k.length)

                    let v = '$'+desc[0].k+'.get'+s+'() '+ desc[2].k
                    let k = desc[0].v+'的 '+desc[1].v+' '+ desc[2].v
                    this.descData[0] = {k:k,v:v}
                    this.descData[1] = {}
                    this.dataForm.rightValue = ''
                }else{
                    this.descData[0] = {k:data.methodDesc,v:'ClmUtil.' +data.methodExpression}
                    this.dataForm.conditionMethodType = data.methodType
                    this.dataForm.leftEntityId = data.entityId
                    if(data.methodParams){
                        this.divisorMethodParams = JSON.parse(data.methodParams)
                    }
                    this.dataForm.methodName = data.methodExpression
                }
                this.showValue()

            },
            // 获取规则因子,实体类
            initData() {
                let params = {
                    is_effect: '1'
                }
                if(this.dataForm.headType==='1'){
                    this.sysbols = constant.ruleConfig.sysbols
                    listConditionDivisorData(params).then(res => {
                        this.conditionDivisorList = res.data.data
                        if(this.dataForm.divisorId){
                            this.conditionDivisorList.forEach(item=>{
                                if(item.id === this.dataForm.divisorId){
                                    this.divisor = item
                                }
                            })
                            if(this.dataForm.conditionType==='4'){
                                if(this.divisor.divisorType==='1' && this.dataForm.methodParams) {//方法
                                    this.divisorMethodParams = JSON.parse(this.dataForm.methodParams)
                                }
                            }
                        }
                    })
                }else{
                    this.sysbols = constant.ruleConfig.actionSysbols
                    listActionDivisorData(params).then(res => {
                        this.conditionDivisorList = res.data.data
                        if(this.dataForm.divisorId){
                            this.conditionDivisorList.forEach(item=>{
                                if(item.id === this.dataForm.divisorId){
                                    this.divisor = item
                                }
                            })
                            if(this.dataForm.actionType==='4'){
                                if(this.divisor.divisorType==='1' && this.dataForm.methodParams) {//方法
                                    this.divisorMethodParams = JSON.parse(this.dataForm.methodParams)
                                }
                            }
                        }
                    })
                }

                if(this.entityList.length>0){
                    this.entityInfos = this.entityList
                }else{
                    entityList(params).then(res => {
                        this.entityInfos = res.data.data
                    })
                }
                if(this.dataForm.conditionType==='2'){
                    if(this.dataForm.methodParams){
                        this.methodParams= JSON.parse(this.dataForm.methodParams)
                        if(this.methodParams.length>0){
                            this.showParams = true
                        }
                    }

                    let params1 = {
                        id: this.dataForm.leftEntityId+''
                    }
                    if(this.dataForm.headType==='1'){
                        // 获取方法
                        getEntityMethod(params1).then(res => {
                            this.methodList = res.data.data
                        })
                    }else{
                        // 获取方法
                        getActionMethodName({}).then(res => {
                            this.methodList = res.data.data
                        })
                    }

                }else if(this.dataForm.conditionType==='1'){//属性
                    let params = {
                        id: this.dataForm.leftEntityId+''
                    }
                    // 获取实体类属性
                    this.leftEntityItemInfos = []
                    entityItemList(params).then(res => {
                        if (res.data.data) {
                            res.data.data.forEach(item => {
                                item['label'] = item.itemName + '--' + item.itemIdentify
                                this.leftEntityItemInfos.push(item)
                            })
                        }
                    })
                    let params1 = {
                        id: this.dataForm.rightEntityId+''
                    }
                    // 获取实体类属性
                    entityItemList(params1).then(res => {
                        this.rightEntityItemInfos = [] // res.data.data
                        if (res.data.data) {
                            res.data.data.forEach(item => {
                                item['label'] = item.itemName + '--' + item.itemIdentify
                                this.rightEntityItemInfos.push(item)
                            })
                        }
                    })
                }else if(this.dataForm.conditionType==='3'){//属性值
                    let params = {
                        id: this.dataForm.leftEntityId+'' + ''
                    }
                    // 获取实体类属性
                    this.leftEntityItemInfos = []
                    entityItemList(params).then(res => {
                        if (res.data.data) {
                            res.data.data.forEach(item => {
                                item['label'] = item.itemName + '--' + item.itemIdentify
                                this.leftEntityItemInfos.push(item)
                            })
                        }
                    })
                }

            },


            // 选择条件类型
            changeConditionType(item) {
                this.dataForm.conditionType = item.value
                // 属性
                if (this.dataForm.conditionType === '1') {
                    this.descData = new Array(5)
                } else if (this.dataForm.conditionType === '3') {
                    // 模型值
                    this.descData = new Array(4)
                }else if (this.dataForm.conditionType === '4') {
                    // 已定义条件
                    this.descData = new Array(2)
                }

                this.inputV = ''
                this.inputRemark = ''
                this.dataForm.sysbol = ''
                this.dataForm.rightValue=''
                this.dataForm.conditionMethodType = ''
                this.dataForm.leftProperty = null
                this.dataForm.rightProperty = null
                this.dataForm.leftEntityId = null
                this.dataForm.rightEntityId = null
                this.dataForm.divisorId = null
                this.methodParams = []
                this.methodList = []
                this.showParams = false
                this.dataForm.conditionExpression = ''
                this.dataForm.conditionDesc = ''
            },
            // 表达式填写完毕 确定
            getExp() {
                let inputValue = this.dataForm.methodName
                if(!this.dataForm.conditionType){
                    this.$message.error('请选择条件类型' )
                    return
                }
                if(this.dataForm.conditionType==='1' || this.dataForm.conditionType==='3'){
                    //模型属性
                    if(!this.dataForm.leftEntityId || !this.dataForm.leftProperty){
                        this.$message.error('请将模型属性补充完整' )
                        return
                    }

                }else if(this.dataForm.conditionType==='2'){
                    if(!this.dataForm.leftEntityId || !this.dataForm.methodName){
                        this.$message.error('请将方法补充完整' )
                        return
                    }
                    this.dataForm.methodParams = JSON.stringify(this.methodParams)
                    this.dataForm.conditionExpression = 'ClmUtil.' +inputValue
                }else if(this.dataForm.conditionType==='4'){
                    //已定义条件
                    if(!this.dataForm.divisorId){
                        this.$message.error('请将条件补充完整' )
                        return
                    }
                    if(this.divisor.divisorType==='1') {//方法
                        let methodP = ''
                        if(this.divisorMethodParams && this.divisorMethodParams.length>0){
                            // for (var i = 0; i < this.divisorMethodParams.length; i++) {
                            //     let item = this.divisorMethodParams[i]
                            //     let value = item.className
                            //     if (!value) {
                            //         this.$message.error('请输入' + item.desc)
                            //         return
                            //     }
                            //     if (item.type === 'int') {
                            //         const digit = /^[0-9]*$/
                            //         if (!digit.test(value)) {
                            //             this.$message.error(item.desc + '只能输入数字')
                            //             return
                            //         }
                            //     } else if (item.type === 'String') {
                            //         value = '"' + value + '"'
                            //     }
                            //     let p = '$' + item.name + '$'
                            //     inputValue = inputValue.replace(p, value)
                            //     methodP = methodP+ value
                            //     if(i<=this.divisorMethodParams.length-2){
                            //         methodP = methodP + ','
                            //     }
                            // }
                            this.dataForm.methodParams = JSON.stringify(this.divisorMethodParams)
                            if (this.divisor.methodType === '0' || this.divisor.methodType === '1' || this.divisor.methodType === '2') {
                                this.dataForm.conditionMethodType = '0'
                            } else if (this.divisor.methodType === '3') {
                                this.dataForm.conditionMethodType = '1'
                            }
                        }
                        this.dataForm.decisionExp = 'ClmUtil.' +inputValue
                        // this.dataForm.conditionDesc = methodP
                    }
                }
                this.visible = false
                // console.log(this.chooseIndex);
                // if(this.chooseIndex!=null){
                //     this.newConditionList[this.chooseIndex] = this.dataForm
                // }else{
                //     this.dataForm.decisionCode = 'D000'+this.conditionList.length+1
                //     this.newConditionList.push(this.dataForm)
                // }
                // console.log(this.methodList,'methodList');
                this.$emit('refreshDataList',this.dataForm)
            },

            // 选择方法
            insertM(data) {
                this.dataForm.conditionExpression = 'ClmUtil.' + data.name
                this.dataForm.methodName =   data.name
                this.dataForm.conditionDesc = data.className
                this.showParams = false
                // 有参数
                if (data.type === '1' || data.type === '3') {
                    if (data.params) {
                        this.showParams = true
                        this.methodParams = data.params
                    }
                }
                if (data.type === '0' || data.type === '1' || data.type === '2') {
                    this.dataForm.conditionMethodType = '0'
                } else {
                    this.dataForm.conditionMethodType = '1'
                }
                // console.log(this.dataForm.conditionMethodType)
            },

            // 显示描述信息
            showValue() {
                this.dataForm.conditionExpression = ''
                this.dataForm.conditionDesc = ''
                this.descData.forEach(item => {
                    if(item.v && item.k){
                        this.dataForm.conditionExpression = this.dataForm.conditionExpression + item.v
                        this.dataForm.conditionDesc = this.dataForm.conditionDesc + item.k + ' '
                    }
                })
            },

            // 属性值选择
            insertV1(data, item) {
                // console.log(data)
                if (item === '1') { // 左
                    let s = data.itemIdentify.substring(0, 1).toLocaleUpperCase() + data.itemIdentify.substring(1, data.itemIdentify.length)
                    let v = 'get' + s + '()'
                    this.descData[1] = {k:data.itemName,v:v}

                    this.dataForm.leftProperty = data.itemId
                    if (this.conditionType === '3') {
                        if (data.enumList && data.enumList.length > 0) {
                            this.enumList = data.enumList
                            this.enumFlag = true
                        } else {
                            this.enumList = []
                            this.enumFlag = false
                        }
                    }
                } else if (item === '2') { // 比较符
                    this.descData[2] = {k:data.zh,v:data.label}

                    this.dataForm.sysbol = data.value
                } else if (item === '3') { // 右

                    let s = data.itemIdentify.substring(0, 1).toLocaleUpperCase() + data.itemIdentify.substring(1, data.itemIdentify.length)
                    let v = 'get' + s + '()'
                    this.descData[4] = {k:data.itemName,v:v}
                    this.dataForm.rightProperty = data.itemId
                }

                this.showValue()
                // console.log(this.dataForm)
            },
            // 条件因子枚举值选择
            insertV2(item) {
                this.descData[2] = {k:item.enumCode,v:this.dataForm.rightValue}
                this.showValue()
            },
            // 输入值
            insertV3() {
                this.descData[4] = {k:this.dataForm.rightValue,v:this.dataForm.rightValue}
                this.showValue()
            },
            //条件因子值输入
            insertV4() {
                this.descData[1] = {k:this.dataForm.rightValue,v:this.dataForm.rightValue}
                this.showValue()
            },
            //条件因子日期值输入
            insertV5() {
                this.descData[1] = {k:this.dataForm.rightValue,v:this.dataForm.rightValue}
                this.showValue()
            },
            init(headType) {
                this.visible = true

                this.newConditionList = []
                this.divisor = {
                    configDate: null,
                    configUser: null,
                    creTime: null,
                    creUserId: null,
                    divisorIdentify: null,
                    divisorMethod: null,
                    divisorName: null,
                    divisorType: null,
                    entityId: null,
                    id: null,
                    isEffect: null,
                    itemId: null,
                    itemIdentify: null,
                    itemTxtLength: null,
                    itemTxtType: null,
                    itemValueType: null,
                    methodDesc: null,
                    methodExpression: null,
                    methodParams: null,
                    methodType: null,
                    remark: null,
                    sysbol: null
                }
                this.dataForm = {
                    headType:headType,
                    conditionType: '4',//默认因子
                    divisorId: null,
                    leftProperty: null,
                    rightProperty: null,
                    leftEntityId: null,
                    rightEntityId: null,
                    sysbol: '',
                    rightValue: '',
                    conditionExpression: '',
                    conditionDesc: '',
                    conditionMethodType: '',
                    methodParams: '',
                    methodName: '',
                }
                this.entityInfos = []
                this.delInfo = []
                this.methodList = []
                this.methodParams = []
                this.divisorMethodParams = []
                this.showParams = false
                this.conditionType = ''

                this.newConditionList = JSON.parse(JSON.stringify(this.conditionList))
                if(this.chooseIndex!=null && this.conditionList.length>0){
                    this.dataForm = this.newConditionList[this.chooseIndex]
                }

                this.initData()
            },

            // 左实体
            changeLeftEntity(item) {
                this.dataForm.leftEntityId = item.entityId
                this.dataForm.leftProperty = null


                this.descData[0] = {k:item.entityName + ' 的',v:'$' + item.entityIdentify + '.'}
                this.descData[1] = ''

                this.showValue()
                let params = {
                    id: item.entityId + ''
                }
                // 获取实体类属性
                this.leftEntityItemInfos = []
                entityItemList(params).then(res => {
                    if (res.data.data) {
                        res.data.data.forEach(item => {
                            item['label'] = item.itemName + '--' + item.itemIdentify
                            this.leftEntityItemInfos.push(item)
                        })
                    }
                    // this.leftEntityItemInfos = res.data.data
                })
            },
            // 右实体
            changeRightEntity(item) {
                this.dataForm.rightEntityId = item.entityId
                this.dataForm.rightProperty = null
                this.descData[3] = {k:item.entityName + ' 的',v:'$' + item.entityIdentify + '.'}
                this.descData[4] = ''
                this.showValue()
                let params = {
                    id: item.entityId + ''
                }
                // 获取实体类属性
                entityItemList(params).then(res => {
                    this.rightEntityItemInfos = [] // res.data.data
                    if (res.data.data) {
                        res.data.data.forEach(item => {
                            item['label'] = item.itemName + '--' + item.itemIdentify
                            this.rightEntityItemInfos.push(item)
                        })
                    }
                })
            },

            changeEntityItem(item) {
                this.curEntity = item
                this.dataForm.leftEntityId = item.entityId

                let params1 = {
                    className: item.pkgName
                }
                if(this.dataForm.headType==='1'){
                    // 获取方法
                    getEntityMethod(params1).then(res => {
                        this.methodList = res.data.data
                    })
                }else{
                    // 获取方法
                    getActionMethodName({}).then(res => {
                        this.methodList = res.data.data
                    })
                }
                this.enumList = []
                this.enumFlag = false
            },


        }
    }
</script>

<style lang="scss">
  .title {
    padding: 10px;
    background-color: rgba(248, 248, 248, 1);
    margin-bottom: 10px;
  }

  .el-table__row .el-form-item__content {
    margin-left: 0 !important;
  }

  .el-table__row .el-form-item {
    margin-bottom: 0 !important;
  }

  .btn-span {
    display: block;
    text-align: center;
    border: 1px solid #dcdfe6;
    border-radius: 5px;
    cursor: pointer;
  }

  .mr10 {
    margin-right: 10px;
  }
</style>
